﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.houseWork.Bll;

namespace CIT.WMS.houseWork
{
    public partial class FrmInvenTory : Form
    {
        #region 变量
        /// <summary>
        /// 当前盘点单号
        /// </summary>
        private string CurrInventoryCode = string.Empty;
        /// <summary>
        /// 当前料盘编码
        /// </summary>
        private string CurrBarCode = string.Empty;
        /// <summary>
        /// 原有总数量
        /// </summary>
        private string TotalQty = string.Empty;
        /// <summary>
        /// 当前操作类型(打开窗体，首次操作是校验盘点单)
        /// </summary>
        OperationTypeEnum.CurrOperationType CurrOperationType = OperationTypeEnum.CurrOperationType.CheckInventory;
        /// <summary>
        /// 条码解析实体
        /// </summary>
        BarcodeUtils obj = new BarcodeUtils();
        /// <summary>
        /// 条码对象
        /// </summary>
        BarcodeUtils.BarObject barobj;
        /// <summary>
        /// 校验结果
        /// </summary>
        string checkMsg = string.Empty;
        #endregion

        #region 构造函数
        public FrmInvenTory()
        {
            InitializeComponent();
            cbx_inventoryCode.DropDownStyle = ComboBoxStyle.DropDownList;
        }
        #endregion

        #region 窗体载入
        private void FrmInvenTory_Load(object sender, EventArgs e)
        {
            //获取未结盘点单
            DataTable dt_inventory = Bll_Inventory_ti.Query(" where Status<>'2'");
            if (dt_inventory.Rows.Count > 0)
            {
                foreach (DataRow dr in dt_inventory.Rows)
                {
                    cbx_inventoryCode.Items.Add(dr["InventoryCode"].ToString());
                }
            }
            lab_msg.Text = "输入盘点单号,按enter键确认";
        }
        #endregion

        #region 下拉框选中项改变
        private void cbx_inventoryCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            //盘点单下拉框选择项改变的时候，要做初始化操作值        
            InitBeginValue();
            txt_input.Text = cbx_inventoryCode.Text;
            if (txt_input.Text.Length > 0)
            {
                lab_msg.Text = "按下enter键确认盘点单号";
                txt_input.Focus();
            }
            else
            {
                lab_msg.Text = "输入盘点单号,按enter键确认";
            }
        }
        #endregion

        #region 初始化操作值
        /// <summary>
        /// 初始化操作值
        /// </summary>
        private void InitBeginValue()
        {
            CurrOperationType = OperationTypeEnum.CurrOperationType.CheckInventory;//把当前操作类型改为校验盘点单
            CurrBarCode = string.Empty;  //初始化当前料盘编码
            txt_input.Text = string.Empty;//初始化输入框
            barobj = new BarcodeUtils.BarObject();//初始化条码实体
        }
        #endregion

        #region 输入框enter事件
        private void txt_input_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (txt_input.Text == string.Empty)
                {
                    if (CurrOperationType == OperationTypeEnum.CurrOperationType.CheckInventory)
                        lab_msg.Text = "输入盘点单号,按enter键校验";
                    else if (CurrOperationType == OperationTypeEnum.CurrOperationType.CheckBarCode)
                        lab_msg.Text = "输入料盘编号,按enter键校验";
                    else if (CurrOperationType == OperationTypeEnum.CurrOperationType.UpdateQty)
                        lab_msg.Text = "输入料盘数量,按enter键确认";
                }
                else
                {

                    string checkMsg = string.Empty;
                    switch (CurrOperationType)
                    {
                        case OperationTypeEnum.CurrOperationType.CheckInventory:
                            InputAction(CurrOperationType);
                            break;
                        case OperationTypeEnum.CurrOperationType.CheckBarCode:
                            InputAction(CurrOperationType);
                            break;
                        case OperationTypeEnum.CurrOperationType.UpdateQty:
                            InputAction(CurrOperationType);
                            break;
                    }
                }
            }
        }
        #endregion

        #region 对应的操作的处理
        /// <summary>
        /// 操作
        /// </summary>
        /// <param name="Type"></param>

        void InputAction(OperationTypeEnum.CurrOperationType Type)
        {
            checkMsg = string.Empty;//初始化检验结果
            if (Type == OperationTypeEnum.CurrOperationType.CheckInventory)//当前操作类型校验盘点单
            {
                #region 校验盘点单
                //当前盘点单
                CurrInventoryCode = txt_input.Text.Trim();
                //校验盘点单
                if (!Bll_Inventory_ti.CheckInventoryCode(CurrInventoryCode, out checkMsg))
                {
                    //不通过
                    lab_msg.Text = checkMsg;
                    txt_input.SelectAll();
                    return;
                }
                //校验通过 当前操作类型更改为校验条码
                CurrOperationType = OperationTypeEnum.CurrOperationType.CheckBarCode;
                lab_msg.Text = "输入料盘编号,按enter键确认";
                txt_input.Text = string.Empty;
                #endregion
            }
            else if (Type == OperationTypeEnum.CurrOperationType.CheckBarCode)//当前操作类型校验盘条码
            {
                #region 校验编码之前先校验盘点单是否已经汇总
                //校验盘点单
                if (!Bll_Inventory_ti.CheckInventoryCode(CurrInventoryCode, out checkMsg))
                {
                    //不通过
                    lab_msg.Text = checkMsg;
                    txt_input.SelectAll();
                    return;
                }
                #endregion
                #region 校验料盘编码
                //当前条码
                CurrBarCode = txt_input.Text.Trim();
                //校验条码
                //barobj = obj.AnalysisBarcode(Utils.AnalysisSerPath, CurrBarCode);
                //if (barobj.CAT != null && barobj.HUE != null && barobj.REF != null)
                //{
                //    barobj.partNo = AnalysisBarCodePN(CurrBarCode);
                //}
                barobj = new BarcodeUtils.BarObject();
                barobj.reelId = txt_input.Text.Trim();
                if (!Bll_InventoryDetail_tid.CheckBarCode(CurrInventoryCode, barobj.reelId, out checkMsg))
                {
                    //不通过(第二、第三种情况) 不存在盘点单中
                    if (!Bll_Inventory_Detail.QueryReelidStatus(barobj.reelId, CurrInventoryCode, out checkMsg))
                    {
                        lab_msg.Text = string.Format("{0},请重扫描料盘编码", checkMsg);
                        txt_input.SelectAll();
                        txt_input.Focus();
                    }
                    else
                    {//ok
                        lab_msg.Text = string.Format("{0},请扫描下一个料盘编码", checkMsg);
                        //盘点完成后更改状态
                        InitBeginValue();
                        CurrOperationType = OperationTypeEnum.CurrOperationType.CheckBarCode;//把当前操作类型改为校验条码
                    }
                    return;
                }
                //第一种情况 存在于盘点单中
                //校验通过 当前操作类型更改为盘点数量
                CurrOperationType = OperationTypeEnum.CurrOperationType.UpdateQty;
                //条码料号
                DataTable dtBarcode = Bll_Inventory_Detail.QueryBarCodeInfo(string.Format(" where SerialNumber='{0}' ", barobj.reelId));
                if (dtBarcode.Rows.Count > 0)
                {
                    barobj.partNo = dtBarcode.Rows[0]["MaterialCode"].ToString();
                }
                txt_input.Text = checkMsg;//原有总数量
                txt_input.SelectAll();
                TotalQty = checkMsg;//原有总数量 
                lab_msg.Text = string.Format("料号：{0} \r\n下一步输入数量，按下enter键确认", barobj.partNo);
                #endregion
            }
            else if (Type == OperationTypeEnum.CurrOperationType.UpdateQty)//当前操作类型盘点料盘数量
            {
                //数量只能输入正整数
                try
                {
                    if (int.Parse(txt_input.Text.Trim()) < 0)
                    {
                        //不通过
                        lab_msg.Text = "数量只能为大于0的数字";
                        txt_input.SelectAll();
                        return;
                    }
                }
                catch
                {
                    //不通过
                    lab_msg.Text = "数量只能为数字";
                    txt_input.SelectAll();
                    return;

                }
                #region 校验编码之前先校验盘点单是否已经汇总
                //校验盘点单
                if (!Bll_Inventory_ti.CheckInventoryCode(CurrInventoryCode, out checkMsg))
                {
                    //不通过
                    lab_msg.Text = checkMsg;
                    txt_input.SelectAll();
                    return;
                }
                #endregion
                #region 确认盘点数量
                if (Bll_Inventory_ti.UpdateBarCodeQty(txt_input.Text.Trim(), TotalQty, CurrInventoryCode, barobj.reelId, barobj.partNo))
                {
                    lab_msg.Text = "盘点成功，请输入下一个料盘编号!";
                    //盘点完成后更改状态
                    InitBeginValue();
                    CurrOperationType = OperationTypeEnum.CurrOperationType.CheckBarCode;//把当前操作类型改为校验条码
                }
                #endregion
            }
        }
        ///// <summary>
        ///// 解析条码
        ///// </summary>
        ///// <param name="barcode"></param>
        ///// <returns></returns>
        //private string AnalysisBarCodePN(string barcode)
        //{
        //    try
        //    {
        //        string[] barSplit = barcode.Split(';');
        //        return barSplit[2].Remove(0, "PN:".Length);
        //    }
        //    catch
        //    {
        //        return string.Empty;
        //    }
        //}
        #endregion
    }
}